package com.syezon.wifikey.ad;

import android.app.Activity;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;
import com.hodanet.appadvplatclient.ReportAgent;
import com.hodanet.appadvplatclient.ad.AdInfo;
import com.syezon.wifikey.channel.ChannelConfig;
import com.syezon.wifikey.config.TTAdManagerHolder;
import com.syezon.wifikey.utils.Utils;


public class FullVideoAd {

    private static final String TAG_LOG = "fullVideoAd";
    private TTAdNative mTTAdNative;
    private TTFullScreenVideoAd mttFullVideoAd;
    private OnFullVideoAdLoadFailedListener onFullVideoAdLoadFailedListener;

    public void setOnFullVideoAdLoadFailedListener(OnFullVideoAdLoadFailedListener onFullVideoAdLoadFailedListener) {
        this.onFullVideoAdLoadFailedListener = onFullVideoAdLoadFailedListener;
    }

    public interface OnFullVideoAdLoadFailedListener{
        void onFullVideoLoadFailed();
    }
    public void loadAd(Activity activity, AdInfo adInfo) {
        try {
            //step1:初始化sdk
            TTAdManager ttAdManager = TTAdManagerHolder.get();
            //step2:(可选，强烈建议在合适的时机调用):申请部分权限，如read_phone_state,防止获取不了imei时候，下载类广告没有填充的问题。
            TTAdManagerHolder.get().requestPermissionIfNecessary(Utils.getApp());
            //step3:创建TTAdNative对象,用于调用广告请求接口
            mTTAdNative = ttAdManager.createAdNative(Utils.getApp());
            loadCsjAd(activity, adInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("SameParameterValue")
    private void loadCsjAd(final Activity activity, final AdInfo adInfo) {
        //step4:创建广告请求参数AdSlot,具体参数含义参考文档
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(adInfo.getCode())
                .setSupportDeepLink(true)
                .setImageAcceptedSize(1080, 1920)
                .setOrientation(TTAdConstant.VERTICAL)//必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
                .build();
        //step5:请求广告
        mTTAdNative.loadFullScreenVideoAd(adSlot, new TTAdNative.FullScreenVideoAdListener() {
            @Override
            public void onError(int code, String message) {
                LogUtils.eTag(TAG_LOG, "onError " + message);
                if(onFullVideoAdLoadFailedListener !=  null){
                    onFullVideoAdLoadFailedListener.onFullVideoLoadFailed();
                }
                ReportAgent.reportAdvSdkFailed(adInfo, ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
            }

            @Override
            public void onFullScreenVideoAdLoad(TTFullScreenVideoAd ad) {
                LogUtils.eTag(TAG_LOG, "onFullScreenVideoAdLoad " + ad);
                mttFullVideoAd = ad;
                mttFullVideoAd.setFullScreenVideoAdInteractionListener(new TTFullScreenVideoAd.FullScreenVideoAdInteractionListener() {
                    boolean reportShow;
                    @Override
                    public void onAdShow() {
                        if (!reportShow) {
//                            SDKReportHelper.reportSDKAdv(SDKReportHelper.SOURCE_CSJ, posId, SDKReportHelper.OP_TYPE_SHOW);
                            ReportAgent.reportAdvShow(adInfo, ChannelConfig.getChannel(), AppUtils.getAppVersionCode());

                            reportShow = true;
                        }
                    }

                    @Override
                    public void onAdVideoBarClick() {
                        ReportAgent.reportAdvSdkVideoClick(adInfo, ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
                    }

                    @Override
                    public void onAdClose() {
                    }

                    @Override
                    public void onVideoComplete() {
                    }

                    @Override
                    public void onSkippedVideo() {
                    }

                });

                mttFullVideoAd.setDownloadListener(new TTAppDownloadListener() {
                    boolean reportDownload;
                    boolean reportInstall;

                    @Override
                    public void onIdle() {

                    }

                    @Override
                    public void onDownloadActive(long l, long l1, String s, String s1) {

                    }

                    @Override
                    public void onDownloadPaused(long l, long l1, String s, String s1) {

                    }

                    @Override
                    public void onDownloadFailed(long l, long l1, String s, String s1) {

                    }

                    @Override
                    public void onDownloadFinished(long l, String s, String s1) {
                        if (!reportDownload) {
                            reportDownload = true;
                            ReportAgent.reportAdvDownload(adInfo, ChannelConfig.getChannel(), AppUtils.getAppVersionCode());

                        }
                    }

                    @Override
                    public void onInstalled(String s, String s1) {
                        if (!reportInstall) {
                            reportInstall = true;
                            ReportAgent.reportAdvInstall(adInfo,ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
                        }
                    }
                });
                mttFullVideoAd.showFullScreenVideoAd(activity);
            }

            @Override
            public void onFullScreenVideoCached() {
            }
        });
    }
}
